<template>
  <div class="subitemsbox">
    <div class="topbox">
      <el-form ref="form" :model="form" label-width="40px" label-position="left">
         <el-row :gutter="20">
          <el-col :span="5">
            <el-form-item label="企业">
              <FilterSelect 
                ref="yonghurefs" 
                :optionlist="yonghulist" 
                :valuekey="{name:'name',value:'id'}"
                @changeselect="changeqiye"
                :defaultvalue="companyId"
                ></FilterSelect>
            </el-form-item>
          </el-col>
          <el-col :span="6" style="margin-top: 4px;display: flex;">
            <el-radio-group v-model="form.dateType" size="small" style="margin-right: 20px;">
              <el-radio-button  label="1">日</el-radio-button>
              <el-radio-button  label="2">月</el-radio-button>
              <el-radio-button  label="3">年</el-radio-button>
            </el-radio-group>
            <el-date-picker v-if="form.dateType=='1'" v-model="form.value1" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" size="small" style="flex: 1;"></el-date-picker>
            <el-date-picker v-else-if="form.dateType=='2'" v-model="form.value2" type="month" value-format="yyyy-MM" placeholder="选择月" size="small" style="flex: 1;"></el-date-picker>
            <el-date-picker v-else v-model="form.value3" type="year" value-format="yyyy" placeholder="选择年" size="small" style="flex: 1;"> </el-date-picker>
          </el-col>
          <el-col :span="13" style="text-align: right;">
            <el-form-item label="">
              <el-button type="primary" size="small" @click="submit">查 询</el-button>
            </el-form-item>
          </el-col>
         </el-row>
      </el-form>
    </div>
    <div class="bodybox">
      <div class="bodytop">
        <div class="bodytopleft">
          <div>用电构成</div>
          <div style="height: 95%;" ref="elegoucheng"></div>
        </div>
        <div class="bodytopright">
          <div class="bodytoprighttitle">
            <span style="margin-right: 50px;">分项用电排名</span>
            <span>
              <el-tabs v-model="activeName" @tab-click="handleClick">
                <el-tab-pane label="暖通" name="1"></el-tab-pane>
                <el-tab-pane label="照明" name="2"></el-tab-pane>
                <el-tab-pane label="动力" name="3"></el-tab-pane>
                <el-tab-pane label="特殊" name="4"></el-tab-pane>
              </el-tabs>
            </span>
          </div>
          <div style="height: 95%;" ref="elepaiming"></div>
        </div>
      </div>
      <div class="bodyfooter">
        <div>用电趋势</div>
        <div ref="eleyongdainqushi" style="height: 95%;"></div>
      </div>
    </div>
  </div>
</template>

<script>
import FilterSelect from '@/components/FilterSelect'
import * as echarts from 'echarts';
import { getyongdianlistapi } from '@/api/energy-consumption/Basic-information/medium'
import {getsubitemgouchengapi,getsubitempaimingapi,getsubitemqushiapi,getdanweidongapi} from '@/api/energy-consumption/Building-energy/Sub-items'
export default {
  name: 'CallPoliceIndex',
  components: {
    FilterSelect
  },
  data() {
    return {
      form:{dateType:'1',type:'elec',danweiname:''},
      yonghulist: [],
      activeName: '1',
      companyId:''
    };
  },

  async mounted() {
    this.gettime()
    // 获取用电单位
    const { data: { list } } = await getyongdianlistapi()
    this.yonghulist = list
    const companyId = localStorage.getItem('companyId')
    this.$set(this, 'companyId', companyId ? +companyId : list[0].id)
    
    this.submit()
  },

  methods: {
    changeqiye(val) {
      this.companyId=val
      localStorage.setItem('companyId', val)
    },
    submit() {
      this.getelegoucheng()
    this.getelepaiming()
    this.getelequshi()
    },
    // 用电排名type切换
    handleClick(val) {
      this.getelepaiming()
    },
    // 获取用电趋势
    async getelequshi() {
      var chartDom = this.$refs.eleyongdainqushi
      var myChart = echarts.init(chartDom);
      var option;
      const { data: { list } } = await getsubitemqushiapi({
        companyId:this.companyId,dateType: this.form.dateType,
        dateString: this.form.dateType == '1' ? this.form.value1 : this.form.dateType == '2' ? this.form.value2 : this.form.value3,
      })
      // 获取X轴数据
      const timePoints =[]
      for (let i = 0; i < list.length; i++) {
          if (list[i].datalist.length > 0) {
              list[i].datalist.forEach(data => {
                timePoints.push(data.timeReading);
              });
              break;
          }
      }
      const series = list.map(ele => {
        return {
            name:ele.name,
            data: ele.datalist.map(ele=>ele.valueReading),
            type: 'bar',
            stack: 'total',
            emphasis: {
              focus: 'series'
            },
            animationDelay: function (idx) {
              return idx * 50 + 1000;
            }
          }
      })
      option = {
        color: ['#f68b00','#9ecd1b',  '#ef5a78', '#f7f1bd'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: "shadow"
          }
        },
        legend: {
          right: '0'
        },
        grid: {
          left: '1%',
          right: '1%',
          bottom: '5%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: timePoints
        },
        yAxis: {
          name:'kWh',
          type: 'value',
        },
        series: series,
        animationEasing: 'elasticOut',
        animationDuration: 2000,
        // barWidth: 60
      };
      myChart.clear();
      option && myChart.setOption(option);
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    },
    // 获取用电排名
    async getelepaiming() {
      var chartDom = this.$refs.elepaiming
      var myChart = echarts.init(chartDom);
      const { data: { list } } = await getsubitempaimingapi({
        companyId:this.companyId,dateType: this.form.dateType,subItemType:this.activeName,
        dateString: this.form.dateType == '1' ? this.form.value1 : this.form.dateType == '2' ? this.form.value2 : this.form.value3,
      })
      const datetime=list.map(ele=>ele.timeReading)
      const datelist=list.map(ele=>ele.valueReading)
      var option;
      option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        color: ['#59e3ff'],
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: datetime,
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name:'(kWh)',
          axisLine: {
              show: true,
            },
          }
        ],
        series: [
          {
            name: '',
            type: 'bar',
            barWidth: '60%',
            data: datelist
          }
        ]
      };
      myChart.clear();
      option && myChart.setOption(option);
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    },
    // 获取用电构成图表
    async getelegoucheng() {
      var chartDom = this.$refs.elegoucheng
      var myChart = echarts.init(chartDom);
      const { data: { list } } = await getsubitemgouchengapi({
        companyId:this.companyId,dateType: this.form.dateType,
        dateString: this.form.dateType == '1' ? this.form.value1 : this.form.dateType == '2' ? this.form.value2 : this.form.value3,
      })
      var option;
      option = {
        tooltip: {
          trigger: 'item'
        },
        color: ['#9ecd1b', '#f68b00', '#ef5a78', '#f7f1bd'],
        legend: {
          orient: 'vertical',
          right: '5%',
          top:'middle'
        },
        series: [
          {
            name: '',
            type: 'pie',
            radius: '80%',
            center:['35%','50%'],
            data: list,
            label: {
              show: false,
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      };
      myChart.clear();
      option && myChart.setOption(option);
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    },
    // 获取当前时间戳
    gettime() {
      const date = new Date()
      const year = date.getFullYear() // 年
      const mon = date.getMonth() + 1 // 月
      const month =mon<10?'0'+mon:mon
      const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() //日
      this.$set(this.form,'value3',year.toString())
      this.$set(this.form,'value2',year + '-' + month)
      this.$set(this.form,'value1',year+'-'+month+'-'+day)
    },
  },
};
</script>

<style scoped>
.subitemsbox{
  height: 100%;
  padding: 10px;
  .topbox{
    height: 80px;
    padding: 20px;
    background-color: #fff;
    margin-bottom: 10px;
  }
  .bodybox{
    height: calc(100% - 90px);
    .bodytop{
      height: 49%;
      margin-bottom: 10px;
      display: flex;
      .bodytopleft{
        width: 35%;
        margin-right: 10px;
        background-color: #fff;
        padding: 20px;
      }
      .bodytopright{
        flex:1;
        padding:20px;
        background-color: #fff;
        .bodytoprighttitle{
          display: flex;
          /* align-items: center; */
        }
      }
    }
    .bodyfooter{
      height: 49%;
        padding: 20px;
      background-color: #fff;
    }
  }
}
::v-deep .el-tabs__item{
  line-height: 0;
  height: 20px;
  /* font-size: 16px; */
}
::v-deep .el-tabs__header{
  margin: 0;
}
::v-deep .el-tabs__nav-wrap::after{

position:static !important;

}
</style>